﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CommunitySystem.PublicOfficialSelectionService;

namespace CommunitySystem
{
    public partial class KaoGuanDetailControl : UserControl
    {
        private PublicOfficialSelectionServiceClient Client = new PublicOfficialSelectionServiceClient();
        private KaoGuanDetailModel model = new KaoGuanDetailModel();
        public ObservableCollection<PublicOfficialInfo> poi = new ObservableCollection<PublicOfficialInfo>();
        public int tiaojian = 0;
        public KaoGuanDetailControl()
        {
            InitializeComponent();
            //for (int i = DateTime.Today.Year; i > DateTime.Today.Year - 10; i--)
            //{
            //    ComboBoxItem cbi = new ComboBoxItem();
            //    cbi.Content = i.ToString();
            //    cbi.Tag = i;
            //    nian.Items.Add(cbi);
            //}
            //nian.SelectedIndex = 0;
            
            Client.GetPublicOfficialInfoCompleted += new EventHandler<GetPublicOfficialInfoCompletedEventArgs>(Client_GetPublicOfficialInfoCompleted);
            Client.GetPublicOfficialInfoAsync(
                @"status = 1
union all 
select * from PublicOfficialInfo where status = 0 order by inputtime desc");
            
        }

        void Client_GetPublicOfficialInfoCompleted(object sender, GetPublicOfficialInfoCompletedEventArgs e)
        {
            poi = e.Result;
            nian.ItemsSource = poi;
            if(poi.Count > 0) nian.SelectedIndex = 0;
            Load();
        }

        public void Load()
        {
            Client.GetKaoGuanDetailModelCompleted += new EventHandler<GetKaoGuanDetailModelCompletedEventArgs>(Client_GetKaoGuanDetailModelCompleted);
            Client.GetKaoGuanDetailModelAsync(name.Text, tiaojian, 0, "选调交流");
        }

        void gangwei_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (((ComboBox) sender).SelectedItem != null)
            {
                tiaojian = ((PublicOfficialInfo) (((ComboBox) sender).SelectedItem)).ID;
            }
        }

        void Client_GetKaoGuanDetailModelCompleted(object sender, GetKaoGuanDetailModelCompletedEventArgs e)
        {
            DGRole.ItemsSource = null;
            DGRole.ItemsSource = e.Result;
        }

        private void NewData_Click(object sender, RoutedEventArgs e)
        {
            var seljw = this.DGRole.SelectedItem as KaoGuanModel;

            //throw new NotImplementedException();
            if (seljw != null)
            {
                KaoGuanDetailWindow RMWindow = new KaoGuanDetailWindow(Convert.ToInt32(seljw.ID));
                RMWindow.Closed += new EventHandler(RMWindow_Closed);
                RMWindow.Show();

            }
            else SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择要编辑的考官信息！"); 
        }
        void RMWindow_Closed(object sender, EventArgs e)
        {
            Load();
        }

        private void DeleteData_Click(object sender, RoutedEventArgs e)
        {
            var seljw = this.DGRole.SelectedItem as KaoGuanModel;
            if (MessageBox.Show("确定删除吗？", "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
            {
                Client.DeteleKaoGuanDetailModelCompleted -= new EventHandler<DeteleKaoGuanDetailModelCompletedEventArgs>(Client_DeteleKaoGuanDetailModelCompleted);
                Client.DeteleKaoGuanDetailModelCompleted += new EventHandler<DeteleKaoGuanDetailModelCompletedEventArgs>(Client_DeteleKaoGuanDetailModelCompleted);
                Client.DeteleKaoGuanDetailModelAsync(Convert.ToInt32(seljw.ID));
            }
        }

        void Client_DeteleKaoGuanDetailModelCompleted(object sender, DeteleKaoGuanDetailModelCompletedEventArgs e)
        {
            Load();
        }

        private void ChaData_Click(object sender, RoutedEventArgs e)
        {
            Load();
        }
    }
}
